<?php
// mod文件只能被入口文件引用，不能直接访问
if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

$subject_list = array();
$subject_list['babyinfo'] = '胎儿生长发育';
$subject_list['mother'] = '孕妈妈身体变化';
$subject_list['father'] = '准爸爸爱妻行动';
$subject_list['noticeinfo'] = '孕期注意事项';
$subject_list['nutrition'] = '营养与保健';
$subject_list['antenatal'] = '胎教';
$subject_list['pregnancyfood'] = '每周推荐孕妇食谱';

$topicid = $_G["gp_topicid"];
$subject = $_G["gp_subject"];

$subject = !in_array($subject, array_keys($subject_list)) ? 'babyinfo' : $subject;
$paragraph_title = $subject_list[$subject];

$pic_path = 'static/image/expert/subject/';
$thumb_path = 'static/image/expert/subject/thumb/';

if(!isset($pic_list) || !is_array($pic_list) || count($pic_list) == 0){
	$pic_list = array();
	$sql = " select  t.id,t.title as week_name,a.id article_id,t.id - 47 as pic_id, a.title as article_title,p.title,p.content 
			from t_topic t, t_article a, t_paragraph p 
			where t.id = a.topic_id and a.id = p.article_id and a.title like '%$paragraph_title' and trim(p.content) != '' ";

	$query = DB::query($sql.' order by id');
	$i = 0;
	$id = -1;
	while($row = DB::fetch($query)) {
		if ($id != $row['id']){
			$pic_list[$i]['desc'] = $row['content'];
			
			$pic_list[$i]['pic'] = $pic_path.(intval($row['pic_id'])).'.jpg';
			$pic_list[$i]['thumb'] = $thumb_path.(intval($row['pic_id'])).'.jpg';
			$pic_list[$i]['picid'] = (intval($row['pic_id']));
			$pic_list[$i]['article_title'] =  $row['article_title'];
			$pic_list[$i]['week_name'] =  $row['week_name'];
	
			$id = $row['id'];
			$i += 1;
		}
		else {
			$pic_list[$i]['desc'] .= ('&lt;br/&gt;'.$row['content']);
		}
	}
	DB::free_result($query);
}

$page_size = 12; 
$page = $_G["gp_page"];
if(isset($page) && $page >= 0 && $page < (count($pic_list) / $page_size + 1) ) {
	$picid = ($page-1) * $page_size + 1;
}
else {
	$page = 1;
}
	
$picid = $_G["gp_picid"];
if(!isset($picid)) {
	if(isset($page) && $page >= 0 && $page < (count($pic_list) / $page_size + 1) ) {
		$picid = ($page-1) * $page_size + 1;
	}
	else {
		$page = 1;
		$picid = 1;
	}
}


$page = intval($page);
$prepage = $page - 1;
$nextpage = $page + 1;

if($prepage < 1){
	$prepage = 1;
}
if($nextpage > (count($pic_list) / $page_size) ){
	$nextpage = (count($pic_list) / $page_size);
	if($nextpage != intval($nextpage)) {
		$nextpage = intval($nextpage) + 1;
	}
}

$pic_thumb_list = array();
$startid =  ($page-1) * $page_size;
for($i = $startid; $i < $startid + $page_size && $i < count($pic_list); $i++ ){
	$pic_thumb_list[] = $pic_list[$i];
}

$picid = intval($picid);
$preid = $picid - 1;
$nextid = $picid + 1;

if($preid < $pic_list[0]["picid"] ){
	$preid = $pic_list[count($pic_list)-1]["picid"];
}

if($nextid > $pic_list[count($pic_list)-1]["picid"]){
	$nextid = $pic_list[0]["picid"];
}

// 图片总数
$piccount = count($pic_list);

$pic = $pic_list[intval($picid) - 1];

include_once template("expert/viewsubject");

?>